package com.za.plugin;

import com.za.plugin.transfer.form.*;
import com.za.plugin.transfer.func.*;

import java.util.ArrayList;
import java.util.List;

public class SQLHelper {
    public static void main(String[] args) {
        System.out.println(processSql("select\n" +
                "    id,\n" +
                "     `type`, \n" +
                "     area_id as areaId,\n" +
                "     area_name as areaName,\n" +
                "      `year`, \n" +
                "      create_id as createId,\n" +
                "       create_time as createTime,\n" +
                "       update_time as updateTime,\n" +
                "     create_name as createName,\n" +
                "     `comment`, \n" +
                "     start_time as startTime, \n" +
                "     end_time as endTime\n" +
                "    from tbl_declare_template\n" +
                "    where\n" +
                "      id=#{id}" ));
    }
    public static String processSql(String sql) {
        List<FunctionTransfer> functionTransferList = initFunctionTransfer();
        List<FormTransfer> formTransferList = initFormTransfer();
        sql = initTrans(sql);

        for (FormTransfer formTransfer : formTransferList) {
            sql = cleanSql(sql);
            if (formTransfer.isSupport(sql)) {
                sql = formTransfer.transfer(sql);
            }
        }
        for (FunctionTransfer functionTransfer : functionTransferList) {
            sql = cleanSql(sql);
            if (functionTransfer.isSupport(sql)) {
                sql = functionTransfer.transfer(sql);
            }
        }
        return cleanSql(sql);
    }

    // SQLUtils#toMySqlString 会转部分sql为大写
    private static String cleanSql(String sql) {
        return sql.toLowerCase().replaceAll("\n", " ").replaceAll("\t", " ").trim();
    }

    // 达梦的 ' 是字符串，" 是列名,先把所有的'转为",再在selectItem部分转'为"
    private static String initTrans(String sql) {
        sql = sql.replace("\n", " ").
                replace("\t", " ").replaceAll("\\s+", " ");
        // #{ id } => #{id}
        sql = sql.replaceAll("#\\s*\\{\\s*", "#{").replaceAll("\\s+}", "}");
        // 去掉 `` 符号
        sql = sql.replace("`", "").replace("\"", "'").toLowerCase();
        return sql.trim();
    }


    private static List<FormTransfer> initFormTransfer() {
        List<FormTransfer> transferList = new ArrayList<>();
        transferList.add(new GroupByAliasFormTransfer());
        transferList.add(new LikeFormTransfer());
        transferList.add(new IfFormTransfer());
        transferList.add(new IfAliasFormTransfer());
        transferList.add(new SelectFormTransfer());
        transferList.add(new SkipGroupBySortFormTransfer());

        transferList.add(new JoinFormTransfer());
        transferList.add(new CaseWhenFormTransfer());
        transferList.add(new SelectHintFormTransfer());
        transferList.add(new TruncateFormTransfer());
        transferList.add(new OrderByIsNullFormTransfer());
        transferList.add(new ChangeDivFormTransfer());
        transferList.add(new EliminateKeywordAlias());

        return transferList;
    }

    private static List<FunctionTransfer> initFunctionTransfer() {
        List<FunctionTransfer> transferList = new ArrayList<>();
        transferList.add(new ConcatFunctionTransfer());
        transferList.add(new StrToDateFunctionTransfer());
        transferList.add(new DateFormatFunctionTransfer());
        transferList.add(new ConvertFunctionTransfer());
        transferList.add(new CountFunctionTransfer());
        transferList.add(new SubdateFunctionTransfer());
        transferList.add(new DateAddFunctionTransfer());
        transferList.add(new DatediffFunctionTransfer());
        transferList.add(new DateFunctionTransfer());
        transferList.add(new DateSubFunctionTransfer());
        transferList.add(new FormatFunctionTransfer());

        transferList.add(new GroupConcatFunctionTransfer());
        transferList.add(new PowFunctionTransfer());
        transferList.add(new RegexpFunctionTransfer());
        transferList.add(new UnixTimeFunctionTransfer());
        transferList.add(new JsonArrayAggFunctionTransfer());
        transferList.add(new FindInSetFormTransfer());


        return transferList;
    }
}
